package com.aiju.cash.ui.enter;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.enter.util.EnterHelper;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.DateUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemTextView;

import java.io.File;

/**
 * @author: 佐为
 * @date: 2017-05-26 09:47
 * @version: V1.0
 * @Description: 经营许可证
 */
public class ManageImageActivity extends BaseTitleActivity {

    private ImageView imageView;
    private File logoFile;

    private SettingItemTextView timeLong, time;
    private String licence_expires;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        setCenterTitle("经营许可证");
        setRightTitle("确认上传");
        licence_expires = getIntent().getStringExtra("licence_expires");

        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (logoFile == null) {
                    T.ToastShort("请选择图片，或替换原有图片");
                    return;
                }
                if (TextUtils.isEmpty(licence_expires)) {
                    T.ToastShort("请设置经营许可证有效期");
                    return;
                }

                Intent intent = new Intent();
                intent.putExtra("licence_expires", licence_expires);
                EnterHelper.uploadImageFile(ManageImageActivity.this, mDialogHelper, intent, logoFile);

            }
        });
        imageView = (ImageView) findViewById(R.id.iv_enter_add);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageDialogUtil.showSelectPicDialog(ManageImageActivity.this);
            }
        });
        EnterHelper.displayImage(getIntent(), mDialogHelper, imageView);

        timeLong = (SettingItemTextView) findViewById(R.id.set_permit_long);
        Drawable drawable = getResources().getDrawable(R.drawable.selector_setting);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        TextView rightView = timeLong.getRightView();
        rightView.setCompoundDrawables(null, null, drawable, null);
        timeLong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                if (v.isSelected()) {
                    time.setVisibility(View.GONE);
                    licence_expires = "长期";
                } else {
                    time.setVisibility(View.VISIBLE);
                    licence_expires = time.getRightString();
                }
            }
        });
        time = (SettingItemTextView) findViewById(R.id.set_permit_time);
        time.setRightText(DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBirthday(time.getRightView(), "选择时间");
            }
        });

        if (!TextUtils.isEmpty(licence_expires)) {
            if (licence_expires.equals("长期")) {
                timeLong.setSelected(true);
                time.setVisibility(View.GONE);
            } else {
                time.setRightText(licence_expires);
            }
        }
    }

    /**
     * 选择日期
     */
    private void showBirthday(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                licence_expires = date;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logoFile = EnterHelper.onActivityResult(imageView, requestCode, resultCode, data);
    }
}
